<?php
require_once("header.php");



if(!$Auth->checkAuth()) // if user isn't logged in
{
    header("location: index.php");
	exit;
}

$template->assign('script', 'leaderboard');

if(isset($_GET['m']))
$m =  safeGet($_GET['m']);	
else
$m = 'today';


$_uid = makesafe($Auth->getLoggedId());

if(isset($_POST['hideUser']))
{
	
	$hu  = makesafe($_POST['hideUser']);
	if($hu != '1')
	$hu = 0;
	
	@mysql_query("UPDATE  users SET l_hideuser = '$hu' WHERE uid = '$_uid' LIMIT 1");
}

if(isset($_POST['hideEarnings']))
{
    
	$hu  = makesafe($_POST['hideEarnings']);
	if($hu != '1')
	$hu = 0;
		
	@mysql_query("UPDATE  users SET l_hideEarnings = '$hu' WHERE uid = '$_uid' LIMIT 1");
		
}






switch($m)
{
    case 'today':
	$msql = "SELECT SUM(credits) as credits, uid  FROM earnings_log WHERE DATE(`date`) = CURDATE() GROUP BY uid ORDER BY credits DESC LIMIT 10";
	break;
	
	case 'month':
	$msql = "SELECT SUM(credits) as credits, uid  FROM earnings_log WHERE MONTH(`date`) = MONTH(CURDATE()) AND YEAR(`date`) = YEAR(CURDATE()) GROUP BY uid ORDER BY credits DESC LIMIT 10";
	break;

}

$que = mysql_query($msql);

if(mysql_num_rows($que))
{
   $x = 1;
	while($r = mysql_fetch_object($que))
	{
	    $credits = $r->credits;
		$uid = $r->uid;
		
		
		$ee = mysql_query("SELECT `l_hideuser`,  `l_hideEarnings` FROM users WHERE uid = '$uid' LIMIT 1");
		$em = mysql_fetch_object($ee);
		$hideUser = $em->l_hideuser;
		$hideEarnings = $em->l_hideEarnings;	

        $__users[] = array('user' => getUserById($uid), 'profit' => $credits, 'rank' => $x, 'hideUser' => $hideUser, 'hideEarnings' => $hideEarnings);
		$x++;
	
	}
	

	$template->assign('users', $__users);
}


		
$ee2 = mysql_query("SELECT `l_hideuser`,  `l_hideEarnings` FROM users WHERE uid = '$_uid' LIMIT 1");
$em2 = mysql_fetch_object($ee2);
$hideUser = $em2->l_hideuser;
$hideEarnings = $em2->l_hideEarnings;	
$template->assign('hideUser', $hideUser);
$template->assign('hideEarnings', $hideEarnings);

		
		


$template->assign('m', $m);
$template->display('leaderboard.tpl.php');


?>